package mop.model.resource;

import java.sql.Timestamp;
import java.text.DateFormat;

import mop.exception.IllegalReservationException;

/**
 * The class to keep track of a reservation
 *  
 * @author	MOP Team 7
 */
public class Reservation implements ReservationInterface
{

	/**
	 * Construct a reservation object
	 * 
	 * @param	resource	The resource for this reservation
	 * @param 	start		The start time in timestamp format
	 * @param 	duration	The duration time in time stamp format
	 * 
	 * @post Resource is set
	 * @post Start date is set
	 * @post End date is set based on duration
	 * 	|getEnd() == new Timestamp(start.getTime() + duration.getTime())
	 */
	public Reservation(Resource resource, Timestamp start, Timestamp duration) 
		throws IllegalReservationException
		{
		this.resource = resource;
		this.start = start;
		this.end = new Timestamp(start.getTime() + duration.getTime());
	}
	
	/***************************************************************************
	 * 							Time
	 ***************************************************************************/
	
	/**
	 * @see ReservationInterface#getStart()
	 */
	public Timestamp getStart()
	{
		return this.start;
	}
	
	/**
	 * @see ReservationInterface#getDuration()
	 */
	public Timestamp getDuration()
	{
		return new Timestamp(getEnd().getTime() - getStart().getTime());
	}
	
	/**
	 * @see ReservationInterface#getEnd()
	 */
	public Timestamp getEnd()
	{
		return end;
	}


	
	/**
	 * Checks whether the reservation overlaps with given time interval
	 * 
	 * @param	start	A start time stamp.
	 * @param	end		A stop time stamp.
	 * 
	 * @return	True if the given time period overlaps with reservation. Otherwise false.
	 */
	public boolean isDuring(Timestamp start, Timestamp end)
	{
		return ( start.before(getEnd()) && end.after(getStart()));
	}
	
	/** 
	 * The start time.
	 */
	private final Timestamp start;
	
	/**
	 * The end time.
	 */
	private final Timestamp end;
	
	
	/***************************************************************************
	 * 							Resource
	 ***************************************************************************/
	
	/**
	 * @see ReservationInterface#getResource()
	 */
	public Resource getResource()
	{
		return this.resource;
	}
	
	/** 
	 * Resource
	 */
	private final Resource resource;
	
	/***************************************************************************
	 * 							Removal
	 ***************************************************************************/
	/**
	 * Remove resource
	 * @effect Removes reservation from resource
	 *  |getResource().removeReservation(this)
	 */
	public void remove(){
		getResource().removeReservation(this);
	}
	
	/***************************************************************************
	 * 							Other methods
	 ***************************************************************************/
	
	
	/**
	 * Returns a string representing this reservation.
	 * 
	 * @return A string representing this reservation.
	 */
	public String toString()
	{
            DateFormat format = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT);
            StringBuffer buffer = new StringBuffer();
            buffer.append("From ");
            buffer.append(format.format(start));
            buffer.append(" till ");
            buffer.append(format.format(getEnd()));
            return buffer.toString();
	}
		
}